package com.pango.system.controller;

import java.util.List;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pango.core.easyui.json.ResultMessage;
import com.pango.core.paginator.support.PageForm;
import com.pango.core.utils.StringUtils;
import com.pango.system.model.TimeTask;
import com.pango.system.services.TimeTaskService;

/**
 * ClassName:TimeTaskController <br/>
 * @author   thierry.fu
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/timeTaskController")
public class TimeTaskController {
	
	@Autowired
	private TimeTaskService timeTaskService;
	
	@Autowired
	private Scheduler scheduler;
	
	@ResponseBody
    @RequestMapping(params = "tasks")
    public List<TimeTask> userView(TimeTask task,PageForm pageform) {
    	return timeTaskService.selectAll(pageform.toPageBounds());
    }
	
    @RequestMapping(params = "list")
    public String userList() {
    	return "task/list";
    }
    
    @RequestMapping(params = "start")
    @ResponseBody
    public ResultMessage start(String taskName) {
    	ResultMessage msg = new ResultMessage();
    	try {
			String[] registedTriggers = scheduler.getTriggerNames(Scheduler.DEFAULT_GROUP);
			if(StringUtils.isIn(taskName, registedTriggers)) {
				this.scheduler.resumeTrigger(taskName, Scheduler.DEFAULT_GROUP);
			} else {
				msg.setMsg("在系统中没有注册该任务");
			}
		} catch (SchedulerException e) {
			msg.setMsg("内部错误");
		}
    	return msg;
    }
    
}
